﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FormatingExercise
{
    public class Fraction : IFormattable
    {
        int numerator;
        int denominator;

        public Fraction(int numerator, int denominator)
        {
            this.numerator = numerator;
            this.denominator = denominator;
        }

        public string ToString(string format, IFormatProvider formatProvider)
        {
            if(format == "N")
            {
                return String.Format("{0}/{1}", numerator, denominator);
            }
            else if (format == "R")
            {
                double num = ((double)numerator) / denominator;
                return String.Format("{0:0.##}", num);
            }
            else
            {
                return this.ToString();
                //throw new FormatException(String.Format("Invalid format: {0}.", format));
            }
        }

        public override string ToString()
        {
            return ((double)numerator / denominator).ToString();
        }

        public int Numerator
        {
            get
            {
                return numerator;
            }
            set
            {
                numerator = value;
            }
        }

        public int Denominator
        {
            get
            {
                return denominator;
            }
            set
            {
                if (value == 0)
                {
                    throw new ArgumentException("Denominator cannot be 0.");
                }

                denominator = value;
            }
        }
    }
}
